package si.cir.gui;

import java.util.ArrayList;
import java.util.List;

import si.cir.R;
import si.cir.entities.Zagovor;
import si.cir.util.WSclient;
import android.app.AlertDialog;
import android.app.ListFragment;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class ZagovorListFragment extends ListFragment {
	
	private List<Zagovor> zagovori;
	private Integer mesto;
	
	private Runnable getZagovoriBackgroundThread = new Runnable() {
		public void run() {
			getZagovoriInBackgroundThread();
		}
	};
	
	private void getZagovoriInBackgroundThread() {
		zagovori = WSclient.getZagovor();
		if(zagovori != null){
			final List<String> naslovi = new ArrayList<String>();
			for(Zagovor novica : zagovori)
				naslovi.add(novica.getNaslov());
			getActivity().runOnUiThread(new Runnable() {
				public void run() {
					setListAdapter(new ArrayAdapter<String>(getActivity(), R.layout.simple_item, naslovi));
					setListShown(true);
				}
			});
		}else{
			getActivity().runOnUiThread(new Runnable() {
				public void run() {
					Toast.makeText(getActivity(), "V tej kategoriji ni objav",
						Toast.LENGTH_LONG).show();
					getActivity().finish();
				}
			});
		}
	}
	
	private Runnable getZagovoriPodrobnostiBackgroundThread = new Runnable() {
		public void run() {
			getZagovoriPodrobnostiInBackgroundThread();
		}
	};
	
	private void getZagovoriPodrobnostiInBackgroundThread() {
		zagovori = WSclient.getZagovor();
		if(zagovori != null){
			final List<String> naslovi = new ArrayList<String>();
			final Zagovor zag = new Zagovor();
			zag.kandidat = zagovori.get(mesto).getKandidat();
			zag.datumInMestoZagovora = zagovori.get(mesto).getDatumInMestoZagovora();
			naslovi.add(zag.getKandidat());
			naslovi.add(zag.getDatumInMestoZagovora());
			getActivity().runOnUiThread(new Runnable() {
				
				public void run() {
				
					setListAdapter(new ArrayAdapter<String>(getActivity(), R.layout.simple_item, naslovi));
					setListShown(true);
				}
			});
		}else{
			getActivity().runOnUiThread(new Runnable() {
				public void run() {
					Toast.makeText(getActivity(), "V tej kategoriji ni objav",
						Toast.LENGTH_LONG).show();
					getActivity().finish();
				}
			});
		}
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		setListShown(false);

		Thread thread = new Thread(null, getZagovoriBackgroundThread, "Background");
		thread.start();
	}


	public void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		builder.setMessage(zagovori.get(position).getDatumInMestoZagovora() + "\n\n" + zagovori.get(position).getKandidat())
		       .setCancelable(true);
		AlertDialog alert = builder.create();
		alert.show();
	}
}
